package feizhou.untils.tools;
/**
 * @Description :
 * @Autor : xiongjinpeng  jpx_011@163.com
 * @Date : 2017年9月1日 下午7:31:49
 * @version :
 */


import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CharUtil {

    public static String comma = "！￥……（）——：；“”‘’《》，。？、";//去掉标点符号

//    public static void main(String[] args) {
//        String[] strArr = new String[]{"wwwmicmiucom", "!@#$%^&*()_+{}[]|\"'?/:;<>,.", "！￥……（）——：；“”‘’《》，。？、", "不要啊", "やめて",
//                "韩佳人", "???"};
//        for (String str : strArr) {
//            System.out.println("===========> 测试字符串：" + str);
////            System.out.println("正则判断结果：" + isChineseByREG(str) + " -- " + isChineseByName(str));
//            //System.out.println("Unicode判断结果 ：" + isChinese(str));
//            System.out.println(isLetter(str));
////            System.out.println("详细判断列表：");
////            char[] ch = str.toCharArray();
////            for (int i = 0; i < ch.length; i++) {
////                char c = ch[i];
////                System.out.println(c + " --> " + (isChinese(c) ? "是" : "否"));
////            }
//        }
//    }

    // 根据Unicode编码完美的判断中文汉字和符号
    private static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
            if (comma.indexOf(String.valueOf(c)) > -1) {//去掉标点符号
                return false;
            }
            return true;
        }
        return false;
    }

    // 完整的判断中文汉字和符号
    public static boolean isChinese(String strName) {
        char[] ch = strName.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (!isChinese(c)) {
                return false;
            }
        }
        return true;
    }

    // 是否是字母
    public static boolean isLetter(String str) {
        if (str == null) {
            return false;
        }
        Pattern pattern = Pattern.compile("^[a-zA-Z]+$");
        return pattern.matcher(str.trim()).find();
    }

    // 只能判断部分CJK字符（CJK统一汉字）
    public static boolean isChineseByREG(String str) {
        if (str == null) {
            return false;
        }
        Pattern pattern = Pattern.compile("[\\u4E00-\\u9FBF]+");
        return pattern.matcher(str.trim()).find();
    }

    // 只能判断部分CJK字符（CJK统一汉字）
    public static boolean isChineseByName(String str) {
        if (str == null) {
            return false;
        }
        // 大小写不同：\\p 表示包含，\\P 表示不包含
        // \\p{Cn} 的意思为 Unicode 中未被定义字符的编码，\\P{Cn} 就表示 Unicode中已经被定义字符的编码
        String reg = "\\p{InCJK Unified Ideographs}&&\\P{Cn}";
        Pattern pattern = Pattern.compile(reg);
        return pattern.matcher(str.trim()).find();
    }

    /**
     * 特殊字符转义，进行数据库查询，可以将' \ 等特殊转义
     *
     * @param name
     * @return
     */
    public static String getRepStr(String name) {
        if (name == null) {
            return name;
        }
        Pattern pattern = Pattern.compile("[']");
        Matcher matcher = pattern.matcher(name);
        name = matcher.replaceAll("''");
        return name;
    }

    /**
     * 解析标签
     * <Page:XXXX><Section:XXXX><Event:XXX><TargetId:xxx>
     *
     * @param label
     * @return
     */
    public static Map<String, String> protocolLabel(String label) {
        if (StringUtil.isEmpty(label)) {
            return null;
        }
        char[] chars = label.toCharArray();
        StringBuilder sb = new StringBuilder();
        boolean flag = false;
        Map<String, String> map = new HashMap<>();
        for (char c : chars) {
            if (c == '<') {
                flag = true;
                continue;
            }
            if (c == '>') {
                flag = false;
                String tag = sb.toString();
                String[] split = tag.split(":");
                map.put(split[0], split[1]);
                sb.setLength(0);
                continue;
            }
            if (flag) {
                sb.append(c);
            }
        }
        return map;
    }

    /**
     * 组装标签。
     *
     * @param map Map:{"Page":XXXX, "Section":XXXX, ...}转换为字符串
     * @return <Page:XXXX><Section:XXXX><Event:XXXX><TargetId:xxxx>
     */
    public static String packLabel(Map<String, String> map) {

        if (null != map && StringUtil.isNotEmpty(map.keySet())) {
            StringBuffer sb = new StringBuffer();
            for (String key : map.keySet()) {
                String value = map.get(key);
                if (StringUtil.isNotEmpty(value))
                    sb.append("<" + key + ":" + map.get(key) + ">");
            }
            return sb.toString();
        }
        return "";
    }


    public static String getMoveRightOneString(String str) {
        if (StringUtil.isEmpty(str)) {
            return null;
        }
        char[] strs = str.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0, size = strs.length; i < size; i++) {
            if (strs[i] == 57) {
                sb.append((char) 48);
            } else if (strs[i] == 122) {
                sb.append((char) 97);
            } else {
                sb.append((char) ++strs[i]);
            }

        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(getMoveRightOneString("30847116c0d0d7c899bd4f10e079d2bc"));
    }
}
